package com.example.yis.adriodversioncode;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Environment;
import android.os.IBinder;


import com.example.yis.config.Global;

import java.io.File;
import java.util.zip.InflaterInputStream;

public class UpdateService extends Service {
    private int titleId = 0;

    //文件存储
    private File updateDir = null;
    private File updateFile = null;

    //通知栏
    private NotificationManager updateNotificationManager = null;
    private Notification.Builder updateNotification = null;
    //通知跳转
    private Intent updateIntent = null;
    private PendingIntent updatePendingIntent = null;

    public UpdateService() {
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //获取传值
        titleId = intent.getIntExtra("titleId", 0);
        //创建文件
        if (android.os.Environment.MEDIA_MOUNTED.equals(android.os.Environment.getExternalStorageState())) {
            updateDir = new File(Environment.getExternalStorageDirectory(), Global.downloadDir);
            updateFile = new File(updateDir.getPath(), getResources().getString(titleId) + ".apk");
        }

        this.updateNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        //设置下载过程中，点击通知栏，回到主界面
        updateIntent = new Intent(this, VersionTest.class);
        updatePendingIntent = PendingIntent.getActivity(this, 0, updateIntent, 0);
        //设置通知栏显示内容
//        updateNotification.icon = R.drawable.arrow_down_float;
        updateNotification = new Notification.Builder(this).setContentTitle("上海地铁")
                .setContentText("0");
        updateNotification.setContentIntent(updatePendingIntent);
//        updateNotification.tickerText = "开始下载";
        //发出通知
        updateNotificationManager.notify(0, updateNotification.build());

        //开启一个新的线程下载，如果使用Service同步下载，会导致ANR问题，Service本身也会阻塞
//        new Thread(new updateRunnable()).start();//这个是下载的重点，是下载的过程

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
